//
//  Tools.m
//  GardenGaugeEcg
//
//  Created by 刘映雄 on 2016/12/14.
//  Copyright © 2016年 刘映雄. All rights reserved.
//

#import "Tools.h"

@implementation Tools

+ (UIViewController*)findVC:(UIView*)tempView{
    UIViewController* view;
    while (tempView) {
        tempView = (UIView*)tempView.nextResponder;
        if ([tempView isKindOfClass:[UIViewController class]]) {
            view = (UIViewController*)tempView;
            return view;
        }
    }
    return view;
}
//获取当前屏幕显示的viewcontroller
+(UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}
+ (CGSize)boundingRectWithSize:(CGSize)size Font:(UIFont *)font Title:(NSString *)text {    
    CGSize newSize = CGSizeMake(SCREEN_Width - 30 *xsIZE, MAXFLOAT);

    NSDictionary *attribute = @{NSFontAttributeName:font};
    CGSize retSize = [text boundingRectWithSize:newSize
                                        options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading | NSStringDrawingUsesDeviceMetrics |NSStringDrawingTruncatesLastVisibleLine
                                     attributes:attribute
                                        context:nil].size;
    NSString *str = @"1";
    CGSize size1 = [str boundingRectWithSize:newSize
                                     options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading | NSStringDrawingUsesDeviceMetrics |NSStringDrawingTruncatesLastVisibleLine
                                  attributes:attribute
                                     context:nil].size;
    
    //数据转string 去掉多余的空格特殊符号 然后转data 转字典
    NSInteger number = 0;
    /**  只需要判断末尾是否有换行  */
    if ([text hasSuffix:@"\n"]) {
        number ++;
    }
    retSize.height += number * size1.height;
    
    return retSize;
}
+ (NSString *)updateTimeForRow:(NSString *)createTimeString {
    // 获取当前时时间戳 1466386762.345715 十位整数 6位小数
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    // 创建歌曲时间戳(后台返回的时间 一般是13位数字)
    NSTimeInterval createTime = [createTimeString longLongValue];
    // 时间差
    NSTimeInterval time = currentTime - createTime;
    
    NSInteger sec = time/60;
    if (sec == 0) {
        return [NSString stringWithFormat:@"刚刚"];
    }
    if (sec<60) {
        return [NSString stringWithFormat:@"%ld分钟前",sec];
    }
    // 秒转小时
    NSInteger hours = time/3600;
    if (hours<24) {
        return [NSString stringWithFormat:@"%ld小时前",hours];
    }
    //秒转天数
    NSInteger days = time/3600/24;
    if (days < 30) {
        return [NSString stringWithFormat:@"%ld天前",days];
    }
    //秒转月
    NSInteger months = time/3600/24/30;
    if (months < 12) {
        return [NSString stringWithFormat:@"%ld月前",months];
    }
    //秒转年
    NSInteger years = time/3600/24/30/12;
    return [NSString stringWithFormat:@"%ld年前",years];
    
}
//转换为 11.20 09.12 时间格式
+ (NSString *)updataForNumberTime:(NSString *)time {
    //转换
    NSTimeInterval time1= [time integerValue];
    NSDate *detaildate =[NSDate dateWithTimeIntervalSince1970:time1];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM.dd HH:mm"];
    NSString *forMatterTime = [dateFormatter stringFromDate:detaildate];
    [forMatterTime substringFromIndex:10];
    
    return forMatterTime;
}
//数据转string 去掉多余的空格特殊符号 然后转data 转字典
+ (NSDictionary *)dataParsingRemove_br:(id)content {
    NSString *str = [[NSString alloc]initWithData:content encoding:NSUTF8StringEncoding];
    NSMutableString *mutStr = [NSMutableString stringWithString:str];
    NSString *string1 = [mutStr stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
    NSString *string = [string1 stringByReplacingOccurrencesOfString:@"<br>" withString:@"\\n"];
    
    NSData *xmlData = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *data = [NSJSONSerialization JSONObjectWithData:xmlData options:NSJSONReadingMutableContainers error:nil];
    return data;
}
+ (CGSize)boundingRectWithSize_1:(CGSize)size Font:(UIFont *)font Title:(NSString *)text {
    
    
    
    NSDictionary *attribute = @{NSFontAttributeName:font};
    CGSize retSize = [text boundingRectWithSize:size
                                        options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading |NSStringDrawingTruncatesLastVisibleLine
                                     attributes:attribute
                                        context:nil].size;
    NSString *str = @"1";
    CGSize size1 = [str boundingRectWithSize:size
                                     options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading |NSStringDrawingTruncatesLastVisibleLine
                                  attributes:attribute
                                     context:nil].size;
    //数据转string 去掉多余的空格特殊符号 然后转data 转字典
    NSInteger number = 0;
    /**  只需要判断末尾是否有换行  */
    if ([text hasSuffix:@"\n"]) {
        number ++;
    }
    retSize.height += number * size1.height;
    return retSize;
}


@end
